<?php
if (! defined ('IN_ZWS')) exit ();
// Ajax Api Action
class AAction extends PublicAction {
	function index() {
		header('Location:' . __APP__ . '/i/');
	}

	function addComment() {
		if (checkQuick(5)) $this -> ajaxReturn('', '发言速度过快', 1);
		$addComment = D('AddComment');
		if ($addComment -> create()) {
			if ($addComment -> add()) {
				$this -> ajaxReturn('', '添加成功', 0);
			} else {
				$this -> ajaxReturn('', $addComment -> getError(), 1);
			}
		} else {
			$this -> ajaxReturn('', $addComment -> getError(), 1);
		}
	}

	function doApplaud() {
		$cid = intval($this -> _post('cid'));
		if (!$cid) $this -> ajaxReturn('', '非法操作！', 1);
		$M = M('ArticleApplaud');
		if ($M -> add(array('uid' => $_SESSION['uid'], 'cid' => $cid, 'ctime' => time()))) {
			$count = $M -> where('cid=' . $cid) -> count();
			$this -> ajaxReturn(array('count' => $count), '成功', 0);
		} else {
			$count = $M -> where('cid=' . $cid) -> count();
			if ($M -> where('cid=' . $cid . ' AND uid=' . $_SESSION['uid']) -> find()) $this -> ajaxReturn(array('count' => $count), '您已赞过', 1);
			$this -> ajaxReturn(array('count' => $count), '未知错误', 2);
		}
	}

	function delComment() {
		$cid = intval($this -> _post('cid'));
		if (!$cid) $this -> ajaxReturn('', '非法操作！', 1);
		$M = M('ArticleComment');
		if ($M -> where('id=' . $cid . ' AND uid=' . $_SESSION['uid']) -> delete()) $this -> ajaxReturn('', '删除成功', 0);
		$this -> ajaxReturn('', '非法操作！', 1);
	}

	function doFav() {
		$aid = intval($this -> _post('aid'));
		if (!$aid) $this -> ajaxReturn('', '非法提交', 2);
		$M = M('ArticleFav');
		if ($M -> where(array('aid' => $aid, 'uid' => $_SESSION['uid'])) -> delete()) $this -> ajaxReturn(array('count' => $M -> where(array('aid' => $aid)) -> count()), '成功取消', 1);
		if ($M -> add(array('aid' => $aid, 'uid' => $_SESSION['uid'], 'ctime' => time()))) $this -> ajaxReturn(array('count' => $M -> where(array('aid' => $aid)) -> count()), '收藏成功', 0);
		$this -> ajaxReturn(array('count' => $M -> where(array('aid' => $aid)) -> count()), '未知错误', 3);
	}

	function test() {
		$article = D('CommentView'); //实例化Comment视图模型
		$articleComments = $article -> where(array('aid' => 1, 'pid' => 0)) -> limit(10) -> select(); //将一级评论查询出来
		foreach($articleComments as $comment) {
			$comments[$comment['id']] = $comment; //将查询出来的一级评论按id赋给comments数组
		}
		$commentID = array_keys($comments); //取出一级评论的id，为下面查询对应的二级评论做准备
		if (!isEmpty($commentID)) {
			foreach($commentID as $id) {
				$sqlArray[] = 'pid=' . $id; //生成查询二级评论所用条件数组
			}
			$sql = implode(' OR ', $sqlArray); //将数组连接，生成对应的SQL条件语句
			$articleComments = $article -> where($sql) -> limit(10) -> select(); //查询对应二级评论
			foreach($articleComments as $comment) {
				$comments[$comment['pid']]['comments'][$comment['id']] = $comment; //按树形排列评论
				$applaudCommentID[] = $comment['id'];
			}
			$applaudCommentID = array_merge($commentID, $applaudCommentID); //取出一二级评论的ID
			$applaud = D('CommentApplaudView'); //实例化CommentApplaud视图模型
			unset($sqlArray); //将之前的sql合并前数组销毁
			foreach($applaudCommentID as $id) {
				$sqlArray[] = 'ArticleComment.id=' . $id; //生成新的用于合并的id子条件数组
			}
			$sql = implode(' OR ', $sqlArray); //生成新的sql条件字句
			$applaudArray = $applaud -> where($sql) -> select(); //找出各评论的赞
			foreach($applaudArray as $applaudId) {
				$applauds[$applaudId['id']][] = $applaudId['uid']; //将找出来的信息按评论ID分组
			}
		}
		dump($comments);
		dump($applauds);
		// echo $article -> getLastSql();
	}
}
